package com.hjm.app.activity;

import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.hjm.app.R;
import com.hjm.ui.comment.CommentFragment;
import com.hjm.ui.comment.EditCommentFragment;
import com.hjm.ui.comment.entity.Comment;
import com.hjm.ui.common.TopBar;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CommentActivity extends BaseActivity implements EditCommentFragment.OnEditCommentListener {
    private static final String PREFERENCE_NAME = "HJMPreferences";
    private int goodsId, storeId, orderId;
    private boolean isNewComment;
    private TopBar topBar;
    private CommentFragment commentFragment;
    private EditCommentFragment editCommentFragment;
    private List<Comment> commentList;
    private ExecutorService executorService = Executors.newSingleThreadExecutor();
    private String TAG = "CommentActivity";

    @Override
    void initView() {
        topBar = findViewById(R.id.top_bar);

        topBar.setOnBackClickListener((v) -> {
            finish();
        });
    }

    @Override
    void initData() {
        goodsId = getIntent().getIntExtra("goodsId", -1);
        storeId = getIntent().getIntExtra("storeId", -1);
        orderId = getIntent().getIntExtra("orderId", -1);
        isNewComment = getIntent().getBooleanExtra("isNewComment", false);
        if (!isNewComment && goodsId != -1) {
            topBar.setTitle("所有商品评价");
            getCommentList(goodsId);
        } else if (isNewComment && storeId != -1) {
            topBar.setTitle("新增店铺评价");
            // 解决无法返回完成页问题
            topBar.setOnBackClickListener((v) -> {
                backToOrderDone();
            });
            showEditFragment("评价店铺");
        } else if (isNewComment && goodsId != -1) {
            topBar.setTitle("新增商品评价");
            showEditFragment("评价商品");
            // 解决无法返回完成页问题
            topBar.setOnBackClickListener((v) -> {
                backToOrderDone();
            });
        }
    }

    @Override
    int getView() {
        return R.layout.activity_comment;
    }

    void showEditFragment(String title) {
        editCommentFragment = EditCommentFragment.newInstance(title);
        editCommentFragment.setOnEditStoreCommentListener(this);
        // 获取FragmentManager
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启一个事务，通过事务来添加Fragment
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 替换FrameLayout中的内容
        transaction.replace(R.id.comment_list_fragment, editCommentFragment);
        // 提交事务
        transaction.commit();
    }

    void getCommentList(int goodsId) {
        commentService.getCommentList(String.valueOf(goodsId),
                () -> {
                    this.commentList = commentService.getCommentList();
                    showCommentFragment(commentList);
                },
                msg -> {
                    Log.d(TAG, "getCommentList: 获取评论列表失败");
                });
    }

    void showCommentFragment(List<Comment> commentList) {
        commentFragment = CommentFragment.newInstance(commentList);
        commentFragment.setOnClickListener((v) -> {
            Log.d(TAG, "showCommentFragment: 点击了新增评论按钮");
            isNewComment = true;
            showEditFragment("评价商品");
        });
        // 隐藏评论按钮
        commentFragment.hideAddButton();
        // 获取FragmentManager
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启一个事务，通过事务来添加Fragment
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 替换FrameLayout中的内容
        transaction.replace(R.id.comment_list_fragment, commentFragment);
        // 提交事务
        transaction.commit();
    }

    @Override
    public void onEditStoreCommentCommit(String comment, int[] rating) {
        Comment newComment = new Comment();
        newComment.setLevel(rating);
        newComment.setText(comment);
        if (isNewComment && storeId != -1) {
            newComment.setStoreId(storeId);
        } else if (isNewComment && goodsId != -1) {
            newComment.setGoodsId(goodsId);
            newComment.setOrderId(orderId);
        }
        Log.d(TAG, "onEditStoreCommentCommit: 开始提交 " + newComment.toString());
        newCommentSubmit(newComment);
    }

    void backToOrderDone() {
        // 解决无法返回完成页问题
        Intent orderIntent = new Intent(this, OrderActivity.class);
        orderIntent.putExtra(OrderActivity.TYPE, OrderActivity.TYPE_ORDER_DONE);
//        orderIntent.putExtra(OrderActivity.ORDER_COMMENT,1);    // 返回一个标志
        orderIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(orderIntent);
        finish();
    }

    void newCommentSubmit(Comment comment) {
        commentService.newComment(comment,
                () -> {
                    Toast.makeText(this, "评价成功", Toast.LENGTH_SHORT).show();
                    finish();
                },
                msg -> {
                    Toast.makeText(this, "评价失败", Toast.LENGTH_SHORT).show();
                });
    }
}
